<template>
  <!-- 添加房产弹框 -->
  <el-dialog :title="formData.id?'编辑房产':'添加房产'" :visible="showDialog" @close="closeFn">
    <el-form ref="form" label-width="120px" :model="formData" :rules="rules">

      <el-form-item label="经纪">
        <el-select v-model="agentSelect" placeholder="请选择">
          <el-option
            v-for="item in agentType"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="分类">
        <el-select v-model="cateTypeSelect" placeholder="请选择">
          <el-option
            v-for="item in cateType"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>

      <el-form-item label="标签">
        <el-tag
          v-for="tag in formData.tags"
          :key="tag"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)"
        >
          {{ tag }}
        </el-tag>
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        />
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 新标签</el-button>
      </el-form-item>

      <el-form-item label="地址">
        <el-row type="flex" justify="start">
          <el-cascader
            v-model="selectedOptions"
            size="large"
            :options="options"
            :props="{ value: 'label' }"
            @change="handleChange"
          />
          <el-input v-model="map" />
        </el-row>
      </el-form-item>

      <el-form-item label="价格">

        <el-input v-model="formData.price" />

      </el-form-item>

      <el-form-item label="描述" prop="desc">
        <el-input v-model="formData.desc" type="textarea" />
      </el-form-item>

      <el-form-item label="介绍图片">
        <ImgUpload ref="photos" :limit="5" />
      </el-form-item>

      <el-form-item label="封面">
        <ImgUpload ref="image" :limit="1" />
      </el-form-item>

    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="end">
        <el-button size="small" @click="closeFn">取消</el-button>
        <el-button size="small" type="primary" @click="confirmFn">确定</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getAgents } from '@/api/agents'
import { getCategory } from '@/api/category'
import { addPropertie, getMap, editPropertie, getPropertieDetial } from '@/api/properties'
import { regionData } from 'element-china-area-data'
export default {
  props: {
    showDialog: {
      // 类型
      type: Boolean,
      // 是否必传
      default: false
    }
  },
  data() {
    return {
      map: '',
      agentType: [],
      cateType: [],
      agentSelect: '',
      cateTypeSelect: '',
      formData: {
        title: '', // 标题
        desc: '', // 介绍
        image: '', // 头像
        agent: '', // 经纪
        address: '', // 地址
        price: '', // 价格
        photos: [], // 图片
        tags: [], // 标签
        category: '' // 分类
      },
      inputVisible: false,
      inputValue: '',
      // 地址下拉框
      options: regionData,
      selectedOptions: [],
      editId: '',
      // 校验规则
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    // 获取分类列表
    const cateType = await getCategory()
    this.cateType = cateType.data
    // 获取经纪列表
    const agentType = await getAgents()
    this.agentType = agentType.data

    await getMap()
    // console.log('地图', myMap)
  },
  methods: {
    // 关闭按钮
    closeFn() {
      this.$emit('update:showDialog', false)
      // 清空表单
      this.formData = {
        title: '',
        desc: '',
        image: '',
        agent: '',
        address: '',
        price: '',
        photos: [],
        tags: [],
        category: ''
      }
      this.$refs.photos.fileList = []
      this.$refs.image.fileList = []
      this.agentSelect = null
      this.cateTypeSelect = null
    },

    // 编辑方法
    async getEditor(id) {
      // 存编辑id
      this.editId = id
      const res = await getPropertieDetial(id)
      console.log(181, res.data, id)
      this.formData = res.data
      this.cateTypeSelect = res.data.category.id
      this.agentSelect = res.data.agent.id
      console.log('1111', res.data.category.title, res.data.agent.name)
      this.$refs.photos.editImgs(res.data.photos)
      this.$refs.image.editOneImg(res.data.image)
    },
    // 弹框确认按钮
    async confirmFn() {
      this.formData.agent = this.agentSelect
      this.formData.category = this.cateTypeSelect
      // 表单验证
      await this.$refs.form.validate()
      // 区分添加跟编辑
      if (this.formData.id) {
        // 编辑函数，待测试
        const photos = this.$refs.photos.fileList.map(item => {
          return item.url
        })
        this.formData.photos = photos
        // 调用refs传参,拿到image图片路径
        this.formData.image = this.$refs.image.fileList[0].url
        console.log('表单内容', this.formData)
        //  调用编辑接口
        await editPropertie({
          ...this.formData,
          address: this.formData.address + this.map
        })
      } else { // 实现新增
        const photos = this.$refs.photos.fileList.map(item => {
          return item.url
        })
        this.formData.photos = photos
        // 调用refs传参,拿到image图片路径
        this.formData.image = this.$refs.image.fileList[0].url
        console.log('表单内容', this.formData)
        await addPropertie({
          ...this.formData,
          address: this.formData.address + this.map
        })
      }

      // 关闭弹窗
      this.$emit('upadateEvent')
      this.closeFn()
      // 更新列表
    },

    // 弹框标签相关函数
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    },

    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.formData.tags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    },
    // 弹框地址相关函数
    handleChange(value) {
      this.formData.address = this.selectedOptions.join('/')
    }

  }
}
</script>

<style>
.el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
